<template>
  <page-header-wrapper :title="false">
    <a-card title="其他客户微信添加管理">
      <a-tabs v-model="tab" type="card" @change="onTabs">
        <a-tab-pane key="10" tab="预加V"></a-tab-pane>
        <a-tab-pane key="20" tab="已加V"></a-tab-pane>
        <a-tab-pane key="5" tab="无微信"></a-tab-pane>
      </a-tabs>
      <div class="table-page-search-wrapper">
        <a-form-model :model="queryParam" layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="资源类型">
                <a-select v-model="queryParam['filter[customer_type]']" placeholder="请选择标记类型">
                  <a-select-option v-for="(item,index) in typeOpts" :key="index" :value="index">{{ item }}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="客户姓名">
                <a-input v-model="queryParam['filter[customer_name]']" placeholder="输入客户姓名"/>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="联系电话">
                  <a-input v-model="queryParam['filter[phone]']" placeholder="输入联系电话"/>
                </a-form-model-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => this.queryParam = {}">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="table-operator">
        <s-table
          bordered
          ref="table"
          size="middle"
          :row-key="(record) => record.id"
          :columns="wechatColumns"
          :data="wechatData"
        >
          <div
            slot="filterDropdown"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-input
              v-ant-ref="c => (searchInput = c)"
              :placeholder="`搜索${column.title}`"
              :value="selectedKeys[0]"
              style="width: 188px; margin-bottom: 8px; display: block;"
              @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
              @pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            />
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters)">
              重置
            </a-button>
          </div>
          <template slot="title">
            <div :style="{ textAlign: 'right' }">
              <a-button v-if="tab == 20" :type="btnStatus == '1' ? 'danger' : 'default'" @click="changeButton('1')">筛选已开单客户</a-button>
            </div>
          </template>
          <template slot="first_type" slot-scope="text">
            {{ typeOpts[text] }}
          </template>
          <template slot="action" slot-scope="text,record">
            <a-button v-if="tab == 10" size="small" type="primary" @click="addWechat(record.id)">已加V</a-button>
            <a-button v-if="tab == 20" size="small" type="primary" @click="$router.push({ name: 'AddContract', query: { customer_id: record.customer_id } })">做合同</a-button>
            <!--            <a-button size="small" type="primary" @click="$router.push({ name: 'OtherDetail', params: { id: record.customer_id } })">客户详情</a-button>-->
            <a-rate v-if="tab == 20" disabled :count="1" :value="record.is_order == 1 ? 1 : 0"></a-rate>
          </template>
        </s-table>
      </div>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { STable } from '@/components'
import customerColumns from '@/columns/customer'
import { getOtherWechat, changeStatus } from '@/api/customer'

export default {
  components: {
    STable
  },
  name: 'OtherWechat',
  data () {
    return {
      tab: '10',
      searchInput: null,
      searchText: '',
      searchedColumn: '',
      wechatColumns: customerColumns.wechat,
      wechatData: parameters => {
        return getOtherWechat(Object.assign(parameters, this.queryParam)).then(res => {
          this.typeOpts = res.result.customerTypeOpts
          return res.result
        })
      },
      typeOpts: {},
      advanced: true,
      queryParam: {},
      btnStatus: ''
    }
  },
  created () {
    this.queryParam['filter[wx_type]'] = this.tab
  },
  methods: {
    handleSearch (selectedKeys, confirm, dataIndex) {
      confirm()
      this.searchText = selectedKeys[0]
      this.searchedColumn = dataIndex
    },
    handleReset (clearFilters) {
      clearFilters()
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    addWechat (id) {
      const app = this
      app.$confirm({
        title: '是否确认客户已通过好友申请',
        onOk: () => {
          changeStatus(id).then(res => {
            app.$refs.table.refresh(true)
          })
        }
      })
    },
    onTabs (key) {
      this.queryParam['filter[wx_type]'] = key
      this.$refs.table.refresh(true)
    },
    changeButton (status) {
      if (this.btnStatus === status) {
        delete this.queryParam['filter[is_order]']
        this.btnStatus = ''
        this.$refs.table.refresh(true)
      } else {
        this.btnStatus = status
        this.queryParam['filter[is_order]'] = status
        this.$refs.table.refresh(true)
      }
    }
  }
}
</script>

<style scoped>

</style>
